package com.ruoyi.personal_owned.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.web.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.personal_owned.domain.PersonalOwnedGift;
import com.ruoyi.personal_owned.service.IPersonalOwnedGiftService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户拥有礼物Controller
 * 
 * @author ruoyi
 * @date 2023-04-08
 */
@Controller
@RequestMapping("/personal_owned/gift")
public class PersonalOwnedGiftController extends BaseController
{
    private String prefix = "personal_owned/gift";

    @Autowired
    private IPersonalOwnedGiftService personalOwnedGiftService;
    @Autowired
    private DictService dictService;

    @RequiresPermissions("personal_owned:gift:view")
    @GetMapping()
    public String gift()
    {
        return prefix + "/gift";
    }

    /**
     * 查询用户拥有礼物列表
     */
    @RequiresPermissions("personal_owned:gift:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PersonalOwnedGift personalOwnedGift)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        personalOwnedGift.setUserId(sysUser.getUserId().intValue());
        startPage();
        List<PersonalOwnedGift> list = personalOwnedGiftService.selectPersonalOwnedGiftList(personalOwnedGift);
        return getDataTable(list);
    }

    /**
     * 导出用户拥有礼物列表
     */
    @RequiresPermissions("personal_owned:gift:export")
    @Log(title = "用户拥有礼物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PersonalOwnedGift personalOwnedGift)
    {
        List<PersonalOwnedGift> list = personalOwnedGiftService.selectPersonalOwnedGiftList(personalOwnedGift);
        ExcelUtil<PersonalOwnedGift> util = new ExcelUtil<PersonalOwnedGift>(PersonalOwnedGift.class);
        return util.exportExcel(list, "用户拥有礼物数据");
    }

    /**
     * 新增用户拥有礼物
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户拥有礼物
     */
    @RequiresPermissions("personal_owned:gift:add")
    @Log(title = "用户拥有礼物", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PersonalOwnedGift personalOwnedGift)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        Integer giftCount = personalOwnedGift.getGiftCount();
        personalOwnedGift.setUserId(sysUser.getUserId().intValue());
        personalOwnedGift.setGiftCount(null);
        List<PersonalOwnedGift> personalOwnedGifts = personalOwnedGiftService.selectPersonalOwnedGiftList(personalOwnedGift);
        if (personalOwnedGifts.size()>0) {
            for (PersonalOwnedGift ownedGift : personalOwnedGifts) {
                ownedGift.setGiftCount(ownedGift.getGiftCount()+giftCount);
                personalOwnedGiftService.updatePersonalOwnedGift(ownedGift);

            }
            return  toAjax(1);
        }else {
            personalOwnedGift.setGiftCount(giftCount);
            return toAjax(personalOwnedGiftService.insertPersonalOwnedGift(personalOwnedGift));
        }


    }

    /**
     * 修改用户拥有礼物
     */
    @RequiresPermissions("personal_owned:gift:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PersonalOwnedGift personalOwnedGift = personalOwnedGiftService.selectPersonalOwnedGiftById(id);
        mmap.put("personalOwnedGift", personalOwnedGift);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户拥有礼物
     */
    @RequiresPermissions("personal_owned:gift:edit")
    @Log(title = "用户拥有礼物", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PersonalOwnedGift personalOwnedGift)
    {
        return toAjax(personalOwnedGiftService.updatePersonalOwnedGift(personalOwnedGift));
    }

    /**
     * 删除用户拥有礼物
     */
    @RequiresPermissions("personal_owned:gift:remove")
    @Log(title = "用户拥有礼物", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(personalOwnedGiftService.deletePersonalOwnedGiftByIds(ids));
    }
}
